package se.perp.c4.control;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import se.perp.c4.control.iface.IGameController;
import se.perp.c4.control.iface.IMessenger;
import se.perp.c4.control.iface.ISpecialCardListener;
import se.perp.c4.model.Actor;
import se.perp.c4.model.Actor.EActor;
import se.perp.c4.model.ActorCollection;
import se.perp.c4.model.Territory;
import se.perp.c4.model.Territory.ETerritory;
import se.perp.c4.model.TerritoryCollection;
import se.perp.c4.net.Message;
import se.perp.c4.ui.GamePiece;
import se.perp.c4.ui.PieceCollection;
import se.perp.c4.ui.PieceOrderer;
import se.perp.c4.ui.SpecialCard;

class GandalfWhiteHandler implements ISpecialCardListener {

    private final TerritoryCollection terrColl;
    private final ActorCollection actorColl;
    private final PieceCollection pieceColl;
    private final IMessenger connection;
    private final IGameController controller;

    public GandalfWhiteHandler(
            final ActorCollection ac,
            final TerritoryCollection tc,
            final PieceCollection pc,
            final IMessenger conn,
            final IGameController control) {
        actorColl = ac;
        terrColl = tc;
        pieceColl = pc;
        connection = conn;
        controller = control;
    }

    public void cardClicked(SpecialCard c) {
        Actor gandalf = actorColl.get(EActor.GANDALF);
        Territory fangorn = terrColl.get(ETerritory.FANGORN);

        assert (fangorn.getOccupantCount() == 0 ||
                (fangorn.isOccupiedByLight() && !fangorn.isFull()));

        terrColl.moveActor(gandalf, ETerritory.FANGORN);

        gandalf.isAlive = true;

        GamePiece gp = pieceColl.get(EActor.GANDALF);
        gp.setVisible(true);

        PieceOrderer.orderIn(terrColl, pieceColl, fangorn);

        c.setEnabled(false);

        controller.nextPlayer();


        connection.sendMessage(Message.newGandalfWhitePlayed());
    }
}
